package com.sevenyear.excelanalyze.listener.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sevenyear.excelanalyze.common.utils.CalculateUtils;
import com.sevenyear.excelanalyze.entity.EvaporationRate;
import com.sevenyear.excelanalyze.entity.PartitionBaseInfo;
import com.sevenyear.excelanalyze.service.EvaporationRateService;
import com.sevenyear.excelanalyze.service.PartitionBaseInfoService;

/**
 * @author ：SevenYear
 * @description：TODO
 * @date ：2021/8/29 21:37
 */
public class EvaporationRateListener extends AnalysisEventListener<EvaporationRate> {

    private EvaporationRateService evaporationRateService;
    private PartitionBaseInfoService partitionBaseInfoService;

    public EvaporationRateListener() {
    }

    public EvaporationRateListener(EvaporationRateService evaporationRateService, PartitionBaseInfoService partitionBaseInfoService) {
        this.evaporationRateService = evaporationRateService;
        this.partitionBaseInfoService = partitionBaseInfoService;
    }

    @Override
    public void invoke(EvaporationRate evaporationRate, AnalysisContext analysisContext) {
        //计算平均值
        float average = CalculateUtils.average(evaporationRate.getEvaporationRateMin(), evaporationRate.getEvaporationRateMax());
        evaporationRate.setEvaporationRateAvg(average);
        //设置分区主键id,分区编码partitionCode,区域编码areaCode
        QueryWrapper<PartitionBaseInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("landform",evaporationRate.getLandform()).eq("clay_thickness",evaporationRate.getClayThickness());
        PartitionBaseInfo partitionBaseInfo = partitionBaseInfoService.getOne(wrapper);
        evaporationRate.setPartitionId(partitionBaseInfo.getId());
        evaporationRate.setPartitionCode(partitionBaseInfo.getPartitionCode());
        evaporationRate.setAreaCode(evaporationRate.getArea()+evaporationRate.getPartitionCode());
        UpdateWrapper<EvaporationRate> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("area_code",evaporationRate.getAreaCode());
        evaporationRateService.saveOrUpdate(evaporationRate,updateWrapper);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
